package com.seesmic.methodgroups
{
	/*
	Copyright 2007 SEESMIC, Inc
	Licensed under the Apache License, Version 2.0 (the "License"); 
	you may not use this file except in compliance with the License. You may obtain a copy of the License at 
	
	http://www.apache.org/licenses/LICENSE-2.0 
	
	Unless required by applicable law or agreed to in writing, 
	software distributed under the License is distributed on an 
	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
	See the License for the specific language governing permissions and limitations under the License.
	*/
	import com.seesmic.ServiceBase;
	import com.seesmic.TokenVO;
	import com.seesmic.VideoVO;
	import com.seesmic.events.SeesmicResultEvent;
	
	import flash.events.Event;
	import flash.net.URLRequestMethod;
	
	public class Videos extends MethodGroup
	{
		public function Videos(service:ServiceBase)
			{
			super(service);
		
		}
		
		
		public function getPublic(offset:Number = 0 , pagesize:Number = 20):void
		{
			MethodGroupHelper.invokeMethod(service,
			onGetPublic, "videos.json", {offset:offset, pagesize:pagesize})
		}
		
		private function onGetPublic(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.VIDEOS_PUBLIC))
			{
				return
			}
			
			var object:* = parse(e.target)
			
			 
			var videos:Array =  parseVideos(object)
			
			
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.VIDEOS_PUBLIC, videos))
		}
		
		public function getInfo(uri:String):void
		{
			
			MethodGroupHelper.invokeMethod(service, 
			onGetInfo, "videos/" + uri + ".json");
		}
		
		private function onGetInfo(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.VIDEOS_GET_INFO)) 
			{	
				return
			}
			
			var object:* = parse(e.target)
			
			var video:VideoVO = new VideoVO();
			video.parse(object);
			if(video.video_id != null)
			{
				service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.VIDEOS_GET_INFO, video)) 
			}
			else
			{
				service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.VIDEOS_GET_INFO, video, false)) 
			}
		}
		
		public function getToken():void
		{
			MethodGroupHelper.invokeMethod(service,
			onGetToken, "tokens.json", null, URLRequestMethod.POST)
		}
		
		private function onGetToken(e:Event):void
		{
			
			if(isError(e, SeesmicResultEvent.VIDEOS_GET_TOKEN))
			{
				return
			}
			
			var object:Object = parse(e.target)
			//trace("TEST##NEW TOKEN ID " + object.id)
			if(object.hasOwnProperty("id"))
			{
			var token:TokenVO = new TokenVO();
			token.created_at = object.created_at
			token.id = object.id;
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.VIDEOS_GET_TOKEN, token))
		
			}
			else
			{
			
				service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.VIDEOS_GET_TOKEN, null, false))
			}
		
		}

		public function postVideoObject(params:Object):void
		{
			MethodGroupHelper.invokeMethod(service,
			onPostVideo, "videos.json", params, URLRequestMethod.POST)
		}
		public function postVideo(token:String, title:String, 
		urlname:String = null, inreplyto:String = null, twitter:Boolean = false,
		isPrivate:Boolean = false, toUserName:String = null, language:String = null):void
		{
			//var tosend:Object = {id:data.videoID, 
		//	title:data.title, description:data.description, twitter:data.twitter, language:data.language}
		
			var params:Object = {id:token, title:title}
			if(urlname != null ) params. blog_id = urlname
			if(inreplyto != null) params. reply_to_video_id  = inreplyto
			if(twitter != null) params.twitter = twitter
			if(isPrivate != null) params.is_private = isPrivate
			if(toUserName != null) params.to_username = toUserName
			if(language != null) params.language = language
			/*
			inreplyto, twitter, isPrivate, toUserName)
			is_private
.to_username
language
			*/
			MethodGroupHelper.invokeMethod(service,
			onPostVideo, "videos.json", params, URLRequestMethod.POST)
			
		}
		
		private function onPostVideo(e:Event):void
		{
			
			if(isError(e, SeesmicResultEvent.VIDEOS_POST_VIDEO))
			{
				return 
			}
			
			var data:Object = parse(e.target)
			trace("video id " + data.video_id)
			if(data.hasOwnProperty("video_id"))
			{
				var video_id:String = data.video_id
				service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.VIDEOS_POST_VIDEO,
				data))
			}
			else
			{
				service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.VIDEOS_POST_VIDEO, null, false))
			}
		}
		 
	}
}